<template>
  <div class="content-container">
    <!-- 搜索栏 -->
    <div ref="adminContentSearch" class="content-search-wrap">
      <el-form 
      	class="content-search"
      	inline
      	size="mini"
      >
        <el-form-item label="部门简称">
          <el-input v-model="query.simpleName" placeholder="部门简称"></el-input>
        </el-form-item>

        <el-form-item>
    		  <el-button size="mini" type="primary">搜索</el-button>
    		  <el-button size="mini" type="danger">清空</el-button>
  		</el-form-item>
      </el-form>
    </div>
    <!-- 工具栏 -->
    <div ref="adminContentTools" class="content-tools-wrap hidden">
      	<div class="tools-left f-l">
      		
      		<el-button-group>
			       <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleAdd">新增</el-button>
			   </el-button-group>

      	</div>
      	<div class="tools-right f-r">
      		
			      <el-tooltip effect="dark" content="刷新" placement="top">
              <el-button size="mini" type="primary" icon="el-icon-refresh" circle @click="refresh"></el-button>
            </el-tooltip>
			
      	</div>
    </div>
    <!-- 内容栏--表格 -->
    <div class="content-table-wrap" v-loading="loading" loading-type="chrome-circle-turn" :style="{height:ResultHeight+'px'}">
    	<el-table
        v-if="data"
        border
    		size="mini"
    		:data="data"
        :height.sync="tableHeight"
        default-expand-all
        :expand-row-keys="['deptId']"
        row-key="deptId"
        :tree-props="{children: 'childrenNodes'}"
    		style="width: 100%"
    	>
    		<el-table-column
		      type="selection"
		      width="55">
		    </el-table-column>

    		<el-table-column
    			fixed
		      	type="index"
		      	:index="tableIndex">
		    </el-table-column>

    		<el-table-column
		      fixed
		      prop="simpleName"
		      label="部门简称"
		      width="150">
		    </el-table-column>

		    <el-table-column
		      prop="fullName"
		      label="部门全称"
		      width="200">
		    </el-table-column>

		    <el-table-column
		      prop="pName"
		      label="上级部门"
		      width="200">
		    </el-table-column>

        <el-table-column
          prop="sort"
          label="排序"
          width="80">
        </el-table-column>

		    <el-table-column
		      prop="description"
		      label="备注"
		      width="200">
		    </el-table-column>

		    <el-table-column
		    	fixed="right"
		      	label="操作"
		      	width="150"
		    >

		      <template slot-scope="scope">
		      	<el-button
		      		size="mini"
		      		type="primary"
		      		@click="handleEdit(scope.$index, scope.row)"
		      	>编辑</el-button>

		      	<el-button
		      		size="mini"
		      		type="primary"
		      		@click="handleDel(scope.$index, scope.row)"
		      	>删除</el-button>

		      	
		      </template>

		    </el-table-column>

    	</el-table> 

      <!-- 分页 -->
      <div ref="adminContentPagination" class="content-pagination-wrap">
        <el-pagination
            background
            :current-page="page.currentPage"
            :page-sizes="pageSizes"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
        </el-pagination>
      </div> 
    </div>

    <!-- 添加编辑 -->
    <dept-edit-dialog
      :visible="editDialogVisible"
      :type.sync="editType"
      :form.sync="editForm"
      @confirm="editDialogConfirm"
      @cancel="editDialogCancel"
    ></dept-edit-dialog>
  </div>

</template>

<script>
import { mapGetters } from 'vuex'
import DeptEditDialog from "@/components/Dialog/deptEditDialog";
import {
  getList,
  getDept,
  remove,
  update,
  add,
} from "@/api/admin/dept";

import { getRolesTree } from "@/api/admin/roles";
import adminMixins from "@/mixins/admin"
export default {
  name: 'dept',
  mixins:[adminMixins],
  components:{
  	DeptEditDialog
  },
  computed: {
    ...mapGetters([
      'permission',
      'userInfo'
    ])
  },
  data(){
  	return{
      //1新增，2修改
      editType:0,
      loading:true,
  		editForm:{},
      query: {
        simpleName:''
      },
  		//修改模态框的显示隐藏
  		editDialogVisible:false,
  		data:null,
  		page: {
    	  pageSize: 10,
    	  currentPage: 1,
    	  total: 0
  	  },
  	  pageSizes:[10,20,30,40,50,100],
  	}
  },

  mounted(){
    //设置dom尺寸
    this.setSize();
  	this.loadData(this.page)
  },
  methods:{
  	//请求table数据
  	loadData(page, params = {}){
  		  this.loading = true;
      	getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {

      		console.log("---views/admin/dept--loadData----------")
      		console.log(res)
        	this.loading = false;
          this.data = res.data.data;
        	//this.data = this.reviseData(res.data.data);

          //console.log(this.reviseData(res.data.data))
        	
      	});
      	
  	},
    reviseData(data){
      var key = 'children';
      var arr = data.slice();

      function toParse(arr){
        arr.forEach(function(item){
          if(item.childrenNodes && Array.isArray(item.childrenNodes)){
            item[key] = item.childrenNodes;
            toParse(item[key])
          }
          delete item.childrenNodes
        })

        return arr;
      }

      return toParse(arr)
    },

  	//表格序号
  	tableIndex(index){
  		return index+1
  	},
    //新增菜单
    handleAdd(){
        this.editForm = null;
        this.editType = 1;
        //模态框显示
        this.editDialogVisible = true;
    },
    //刷新当前页
    refresh(){
      this.loadData(this.page)
    },
  	//点击操作的编辑
  	handleEdit(index,row){
      delete row.childrenNodes;
      this.editType = 2;
      this.editForm = row;
      //模态框显示
      this.editDialogVisible = true;
  	},
  	//点击操作的删除
  	handleDel(index,row){

  		this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          	confirmButtonText: '确定',
          	cancelButtonText: '取消',
          	type: 'warning'
        }).then(()=>{
        	return remove(row.deptId);
        }).then((res) => {

        	this.loadData(this.page)
          	this.$message({
            	type: 'success',
            	message: '删除成功!'
          	});
        }).catch(() => {
          	this.$message({
            	type: 'info',
            	message: '已取消删除'
          	});          
        });
  	},
  	
  	editDialogCancel(){
      this.editType = 0;
  		this.editDialogVisible = false;
  		this.editForm = null;
  	},
    editDialogConfirm(form){

      if(form.type == 1){

        add(form.data).then((res)=>{

          this.editDialogVisible = false;
          this.loadData(this.page)
              this.$message({
                type: 'success',
                message: '添加成功!'
              });
        })

      }else if(form.type == 2){

        update(form.data).then((res)=>{
          this.editFrom = null;
          this.editDialogVisible = false;
          this.loadData(this.page)
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
        })
      }
    }  
  }
}
</script>

<style lang="scss" scoped>
.content {
  &-container {

  }
  /***搜索栏***/
  &-search-wrap{
    padding: 15px;
    border-bottom: 1px solid #E8EBEE;
  }
  /***工具栏***/
  &-tools-wrap{
    padding: 15px;
  }
  /***内容栏***/
  &-table-wrap{
    position: relative;
    padding: 15px;
  }
  /***分页***/
  &-pagination-wrap{
    text-align: right;
  	padding-top: 15px;
  }
}
</style>
